package com.linko.intelligent.sys.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linko.intelligent.common.util.UserContext;
import com.linko.intelligent.sys.core.mapper.NoticeMapper;
import com.linko.intelligent.sys.core.service.NoticeService;
import com.linko.intelligent.sys.core.service.UserService;
import com.linko.intelligent.sys.pojo.dto.NoticeDto;
import com.linko.intelligent.sys.pojo.po.Notice;
import com.linko.intelligent.sys.pojo.query.NoticeQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yuan
 * @create 2024/3/15 11:42
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {


    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private UserService userService;

    @Override
    public Long save(NoticeDto dto) {
        Notice po = new Notice();
        BeanUtils.copyProperties(dto, po);
        po.setCreateTime(new Date());
        po.setDel(0L);
        save(po);
        return po.getId();
    }

    @Override
    public void saveList(List<NoticeDto> dtos) {
        for (NoticeDto dto : dtos) {
            save(dto);
        }
    }

    @Override
    public Boolean delete(Long id) {
        Notice po = new Notice();
        po.setId(id);
        po.setDel(1L);
        return updateById(po);
    }

    @Override
    public Boolean update(Long id, NoticeDto dto) {
        Notice po = new Notice();
        BeanUtils.copyProperties(dto, po);
        po.setId(id);
        return updateById(po);
    }

    @Override
    public Notice getDetail(Long id) {
        Notice po = getById(id);
        return po;
    }

    @Override
    public List<Notice> queryList(NoticeQuery query) {
        List<Notice> list = noticeMapper.selectLs(query, UserContext.getUserSession());
        return list;
    }


    @Override
    public PageInfo<Notice> queryPage(NoticeQuery query) {
        PageInfo<Notice> pageInfo = PageHelper.startPage(query.getPn(), query.getPs()).doSelectPageInfo(
                () -> queryList(query)
        );
        return pageInfo;
    }

}
